package cn.faster.common.result;

import cn.faster.common.enums.CodeEnum;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;

/**
 * @ClassName: ResultBean
 * @Description: 接口数据返回封装
 * @Author: faster
 * @CreateDate: 2020/4/2 14:36
 * @Version: 1.0
 */
@Data
public final class ResultBean<T> implements Serializable {


    private static final long serialVersionUID = 7711286170331714067L;

    @ApiModelProperty(value = "状态码：0成功，其它失败")
    private String code;
    @ApiModelProperty(value = "描述信息")
    private String msg;

    @ApiModelProperty(value = "返回数据")
    private Object data = null;

    public ResultBean(String code, String message) {
        this.code = code;
        this.msg = message;
    }

    public ResultBean(CodeEnum codeEnum, T data) {
        this.code = codeEnum.getCode();
        this.data=data;
    }

    public ResultBean(CodeEnum codeEnum) {
        this.code = codeEnum.getCode();
        this.msg = codeEnum.getDescription();
    }

    private ResultBean(String code, T data) {
        this.code = code;
        this.data = data;
    }

    private ResultBean(String code, String message, T data) {
        this.code = code;
        this.msg = message;
        this.data = data;
    }

    public ResultBean() {

    }

    /**
     * 放入自定义业务数据集合并返回自己
     */
    public ResultBean<T> fillData(Object data) {
        this.data = data;
        return this;
    }

    public static <T>ResultBean<T> of(CodeEnum codeEnum) {
        return new ResultBean(codeEnum);
    }

    public static <T> ResultBean<T> SUCCESS() {
        return new ResultBean<T>(CodeEnum.OK);
    }

    public static <T> ResultBean<T> SUCCESS(String message) {
        return new ResultBean<T>(CodeEnum.OK.getCode(), message);
    }

    public static <T> ResultBean<T> SUCCESS(T data) {
        return new ResultBean<T>(CodeEnum.OK, data);
    }

    public static <T> ResultBean<T> SUCCESS(String message, T data) {
        return new ResultBean<T>(CodeEnum.OK.getCode(), message, data);
    }

    public static <T> ResultBean<T> ERROR() {
        return new ResultBean<T>(CodeEnum.SYSTEM_EXECUTION_ERROR);
    }

    public static <T> ResultBean<T> ERROR(String message) {
        return new ResultBean<T>(CodeEnum.SYSTEM_EXECUTION_ERROR.getCode(), message);
    }

    public static <T> ResultBean<T> ERROR(CodeEnum resultCode, String message) {
        return new ResultBean<T>(resultCode.getCode(), message);
    }

    public static <T> ResultBean<T> ERROR(T data) {
        return new ResultBean<T>(CodeEnum.SYSTEM_EXECUTION_ERROR, data);
    }

    public static <T> ResultBean<T> ERROR(CodeEnum resultCode) {
        return new ResultBean<T>(resultCode);
    }

    public static <T> ResultBean<T> VALIDATE() {
        return new ResultBean<T>(CodeEnum.REQUIRED_PARAMETERS_ARE_EMPTY);
    }

    public static <T> ResultBean<T> VALIDATE(String message) {
        return new ResultBean<T>(CodeEnum.REQUIRED_PARAMETERS_ARE_EMPTY.getCode(), message);
    }

    public static <T> ResultBean<T> VALIDATE(T data) {
        return new ResultBean<T>(CodeEnum.REQUIRED_PARAMETERS_ARE_EMPTY, data);
    }

    public static <T> ResultBean<T> BUILD() {
        return new ResultBean<T>();
    }

    public static <T> ResultBean<T> BUILD(String code, String message) {
        return new ResultBean<T>(code, message);
    }

    public static <T> ResultBean<T> BUILD(String code, T data) {
        return new ResultBean<T>(code, data);
    }

    public static <T> ResultBean<T> BUILD(String code, String message, T data) {
        return new ResultBean<T>(code, message, data);
    }

    public boolean hasSuccess() {
        return CodeEnum.OK.getCode().equals(this.getCode());
    }

    public boolean hasError() {
        return !hasSuccess();
    }

}
